import { format as prettify } from 'prettier';
import { ESLint } from 'eslint';
// 格式化代码
export function formatCode(code: string): Promise<any> {
  return prettify(code, {
    parser: 'typescript',
    printWidth: 150,
    tabWidth: 2,
    useTabs: false,
    semi: true, //语句末尾使用分号
    vueIndentScriptAndStyle: true,
    singleQuote: true, // 使用单引号
    quoteProps: 'as-needed',
    bracketSpacing: true,
    trailingComma: 'es5',
    jsxSingleQuote: false,
    arrowParens: 'always',
    insertPragma: false,
    requirePragma: false,
    proseWrap: 'never',
    htmlWhitespaceSensitivity: 'strict',
    endOfLine: 'auto',
    rangeStart: 0,
  });
}

export const eslitCode = async (testCode: string): Promise<any> => {
  // 1. 创建实例
  const eslint = new ESLint({
    useEslintrc: false,
    overrideConfig: {
      extends: ['eslint:recommended'],
      parserOptions: {
        sourceType: 'module',
        ecmaVersion: 'latest',
      },
      env: {
        es2022: true,
        node: true,
      },
      rules: {
        'no-undef': 'off',
      },
    },
  });
  // 2. 检查文本
  const results = await eslint.lintText(testCode);
  // 3. 格式化结果
  const formatter = await eslint.loadFormatter('stylish');
  return formatter.format(results);
};
